# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import logging
import contextlib
import hashlib
import sys
import time

from multidelivery.worker_service import Worker
from taskflow import task
from taskflow.examples import example_utils as eu


BASE_SHARED_CONF = {
    'exchange': 'taskflow',
    'topic': 'test-tasks',
    'url': 'amqp://admin:admin@10.200.2.187:5672//'
}

logging.basicConfig(level=logging.INFO)


@contextlib.contextmanager
def slow_down(how_long=0.5):
    try:
        yield how_long
    finally:
        if len(sys.argv) > 1:
            # Only both to do this if user input provided.
            print("** Ctrl-c me please!!! **")
            time.sleep(how_long)


class PrintText(task.Task):
    """Just inserts some text print outs in a workflow."""
    def __init__(self, name, no_slow=False):
        super(PrintText, self).__init__(name=name)
        self._text = name
        self._no_slow = no_slow

    def execute(self):
        if self._no_slow:
            eu.print_wrapped(self._text)
        else:
            with slow_down():
                eu.print_wrapped(self._text)


class BmBuilder(task.Task):

    def execute(self,):
        print("bm 交付中")
        time.sleep(1)
        bm_info = {
            "count": 2,
            "flavor": "计算型"
        }
        print(bm_info)


class BmSuccess(task.Task):

    def execute(self,):
        print("交付完成")


class AliDeveilder(task.Task):

    def execute(self):
        print("ali yun 交付中")
        time.sleep(1)
        print("ali yun 交付完成")


def run_worker():
    worker_conf = {
        'tasks': [
            PrintText,
            BmBuilder,
            BmSuccess,
            AliDeveilder
        ],
    }
    worker_conf.update(BASE_SHARED_CONF)
    w = Worker(**worker_conf)
    w.start()


if __name__ == '__main__':

    run_worker()
